package com.ccrfid.rmc6.resource.business.visitroom;

import com.ccrfid.rmc6.appconfig.auth.Secured;
import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.entity.business.visitroom.RoomVisitAreaOverallEntity;
import com.ccrfid.rmc6.entity.business.visitroom.RoomVisitRuleEntity;
import com.ccrfid.rmc6.entity.personnel.PersonnelView;
import com.ccrfid.rmc6.pojo.dto.visitroom.VisitRoomCompletionRateDto;
import com.ccrfid.rmc6.pojo.dto.visitroom.VisitRoomResultDto;
import com.ccrfid.rmc6.service.business.visitroom.RoomDutyVisitorService;
import com.ccrfid.rmc6.service.business.visitroom.RoomVisitRuleService;
import com.ccrfid.rmc6.service.business.visitroom.VisitRoomService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.sql.Date;
import java.util.List;

@Secured
@Path("visit-room")
@Produces(MediaType.APPLICATION_JSON)
public class VisitRoomResource {
    @Autowired
    private RoomVisitRuleService roomVisitRuleService;
    @Autowired
    private RoomDutyVisitorService roomDutyVisitorService;
    @Autowired
    private VisitRoomService visitRoomService;

    //获取下监室规则
    @GET
    @Path("rules")
    public List<RoomVisitRuleEntity> getAllVisitRules() {
        return roomVisitRuleService.getAllVisitRules();
    }

    //获取某个下监室规则
    @GET
    @Path("rules/{ruleId}")
    public RoomVisitRuleEntity getVisitRule(@PathParam("ruleId") Long ruleId) {
        return roomVisitRuleService.getVisitRule(ruleId);
    }

    //规则不做添加和删除，只做修改
    //更新某个规则
    @PUT
    @Path("rules/{ruleId}")
    @Consumes(MediaType.APPLICATION_JSON)
    public RoomVisitRuleEntity updateVisitRule(@PathParam("ruleId") Long ruleId, RoomVisitRuleEntity entity)
            throws AppException {
        return roomVisitRuleService.updateVisitRule(ruleId, entity);
    }

    //获取某个监室的下监室责任人
    @GET
    @Path("{orgId}/duty-visitors")
    public List<PersonnelView> getRoomDutyVisitors(@PathParam("orgId") Long orgId) {
        return roomDutyVisitorService.getRoomDutyVisitors(orgId);
    }

    //给某个监室添加一个下监室责任人
    @PUT
    @Path("{orgId}/duty-visitor/{personnelId}")
    public PersonnelView updateRoomDutyVisitor(@PathParam("orgId") Long orgId,
                                               @PathParam("personnelId") Long personnelId) throws AppException {
        return roomDutyVisitorService.updateRoomDutyVisitor(orgId, personnelId);
    }

    //删除某个监室的某个下监室责任人
    @DELETE
    @Path("{orgId}/duty-visitor/{personnelId}")
    public Boolean deleteRoomDutyVisitor(@PathParam("orgId") Long orgId,
                                         @PathParam("personnelId") Long personnelId) throws AppException {
        return roomDutyVisitorService.deleteRoomDutyVisitor(orgId, personnelId);
    }

    //获取某天的下监室统计信息,date:2018-07-01
    @GET
    @Path("{date}/statistics")
    public List<VisitRoomResultDto> getVisitRoomStatistics(@PathParam("date") String dateStr) {
        return visitRoomService.getVisitRoomStatistics(dateStr);
    }

    //重新统计某天的下监室统计信息,date:2018-07-01
    @PUT
    @Path("{date}/statistics")
    public Boolean updateRoomVisitRecords(@PathParam("date") Date date) {
        visitRoomService.updateRoomVisitRecords(date);
        return true;
    }

    @GET
    @Path("prison-area/{date}/completion-rate")
    public List<RoomVisitAreaOverallEntity> getRoomVisitAreaCompletionRate(@PathParam("date") Date date) {
        return visitRoomService.getRoomVisitAreaCompletionRate(date);
    }

    @GET
    @Path("prison-area/average-completion-rate")
    public List<VisitRoomCompletionRateDto> getRoomVisitAverageCompletionRate(@QueryParam("startDate") Date startDate,
                                                                              @QueryParam("stopDate") Date stopDate) {
        return visitRoomService.getRoomVisitAverageCompletionRate(startDate, stopDate);
    }
}
